#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, urllib, re 

try:
        from PyQt4 import QtCore, QtGui
except(ImportError):
        print "Sisteminizde PyQt4 modülü kurulu değil, lütfen kurun ve tekrar deneyin..."
        sys.exit()

class sozluk:
    def __init__(self, ui):
        self.ui = ui
        
    def kelime_ara(self):
        
        kelime = unicode(ui.lineEdit.text()).encode('utf-8') 
        
        # Eğer arama kutusuna bir şey yazılmamışsa uyarı mesajı gösterilcek.
        if len(kelime) == 0 :
            mesaj = QtGui.QMessageBox()
            mesaj.setWindowTitle(unicode('Uyarı', 'utf-8')) 
            mesaj.setText(unicode('Arama kutusuna hiçbir şey yazmadınız !', 'utf-8'))
            mesaj.exec_()
        
        # Eğer arama kutusuna bir şey yazılmışsa kelime araması yapılacak.
        elif len(kelime) != 0 :
            sitede_ara = "http://www.seslisozluk.com/?word=" + kelime     
            site = urllib.urlopen(str(sitede_ara))
            veri = site.read()
        
            metin_ara = re.search('<b>1.(.*)<br></', veri).group(0)
            ui.textEdit.setText(unicode(metin_ara, 'iso-8859-9'))  

    def temizle(self):
        ui.lineEdit.setText('')
        ui.textEdit.setText('')

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(480,330)
        MainWindow.setMinimumSize(QtCore.QSize(480,330))
        MainWindow.setMaximumSize(QtCore.QSize(480,330))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(20,10,301,25))
        self.lineEdit.setMaximumSize(QtCore.QSize(301,25))
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(340,8,111,33))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setWeight(50)
        font.setBold(False)
        self.pushButton.setFont(font)
        self.pushButton.setIconSize(QtCore.QSize(32,25))
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtGui.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(20,50,301,251))
        self.textEdit.setAcceptRichText(False)
        self.textEdit.setObjectName("textEdit")
        self.verticalScrollBar = QtGui.QScrollBar(self.centralwidget)
        self.verticalScrollBar.setGeometry(QtCore.QRect(302,51,17,249))
        self.verticalScrollBar.setMaximumSize(QtCore.QSize(17,249))
        self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
        self.verticalScrollBar.setObjectName("verticalScrollBar")
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(340,60,111,33))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setWeight(50)
        font.setBold(False)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setIconSize(QtCore.QSize(32,25))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(340,230,111,33))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setWeight(50)
        font.setBold(False)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setIconSize(QtCore.QSize(32,25))
        self.pushButton_3.setObjectName("pushButton_3")
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(330,280,161,18))
        font = QtGui.QFont()
        font.setPointSize(8)
        font.setWeight(75)
        font.setBold(True)
        self.label.setFont(font)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.sozluk = sozluk(self)
        QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL("clicked()"),self.sozluk.kelime_ara)
        QtCore.QObject.connect(self.pushButton_2,QtCore.SIGNAL("clicked()"),self.sozluk.temizle)
        QtCore.QObject.connect(self.pushButton_3,QtCore.SIGNAL("clicked()"),MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Tilki", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Ara", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setShortcut(QtGui.QApplication.translate("MainWindow", "Return", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setText(QtGui.QApplication.translate("MainWindow", "Temizle", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setShortcut(QtGui.QApplication.translate("MainWindow", "Ctrl+B", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_3.setText(QtGui.QApplication.translate("MainWindow", "Çıkış", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("MainWindow", "http://tilki.googlecode.com", None, QtGui.QApplication.UnicodeUTF8))
        
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())